let app = getApp();
import Dialog from '../../../dist/dialog/dialog';
import util from "../../../utils/util";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    verifierList:[],
    storeId:0,
    isLoadMore: true,
    page:1,
    totalPage:0,
    keyword:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if((options.storeId??'') !== '') {
      this.setData({
        storeId:options.storeId
      })
    } 
    
  },
  onChange({ detail }) {
    // 需要手动对 checked 状态进行更新
    this.setData({ checked: detail });
  },
  onSearchChange({ detail }) {
    this.setData({ keyword: detail });
    // 防抖
    if (this.timer) clearTimeout(this.timer);
    this.timer = setTimeout(() => {
      util.debounce(this.verifierList());
    }, 500);
  },
  verifierList() {
    let urlStr = app.getNetAddresss("plugin.coupon-store.frontend.clerk-manage.clerkList");
    let json = {
      cstore_id:this.data.storeId,
      page: this.data.page,
      kwd:this.data.keyword
    };
    console.log(json);
    app._postNetWork({
      url: urlStr,
      data: json,
      success: (resdata) => {
        let res = resdata.data;
        let list = res.data.data;
        list.forEach((item) => {
          if(item.status) {
            item.checked = true
          } else{
            item.checked = false
          }
          
        })
        console.log(list);
        wx.hideLoading();
        wx.hideNavigationBarLoading();//隐藏导航条加载动画
        //停止下拉刷新
        wx.stopPullDownRefresh();
        if (res.result == 1) {
          this.data.isLoadMore = true;
          this.data.totalPage = res.data.last_page;
          if (!this.data.totalPage) {
            this.data.totalPage = 0;
          }
          this.setData({
            verifierList : list,
          })
        } else {
          this.data.page = this.data.page - 1;
          this.isLoadMore = false;
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  getMoreData() {
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.totalPage) {
      return;
    } else {
      this.data.page = this.data.page + 1;
      let urlStr = app.getNetAddresss("plugin.coupon-store.frontend.clerk-manage.clerkList");
      let json = {
        cstore_id:this.data.storeId,
        page: this.data.page,
        kwd:this.data.keyword
      };
      app._getNetWork({
        url: urlStr,
        data: json,
        success: (resdata) => {
          let res = resdata.data;
          let list = res.data.data;
          list.forEach((item) => {
            item.checked = true
          })
          if (res.result == 1) {
            this.data.isLoadMore = true;
            this.setData({
              verifierList : this.data.verifierList.concat(list),
            })
          } else {
            this.data.page = this.data.page - 1;
            this.isLoadMore = false;
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1500
            });
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  },
  verifierUpdate(event) {
    let id = JSON.stringify(event.currentTarget.dataset.id);
    wx.navigateTo({
      url: `/packageF/consumption/verifier/verifier?storeId=${this.data.storeId}&itemId=${id}`
    })
  },
  verifierDelete(event) {
    let id = event.currentTarget.dataset.id;
    let that = this;
    let urlStr = app.getNetAddresss("plugin.coupon-store.frontend.clerk-manage.deleteClerk");
    let json = {
      cstore_id:this.data.storeId,
      id:id
    };
    Dialog.confirm({
      title: '提示',
      message: '是否确认删除核销员',
    })
    .then(() => {
      app._postNetWork({
        url: urlStr,
        data:json,
        success: function (resdata) {
          let res = resdata.data;
          if (res.result == 1) {
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1500
            });
            that.setData({
              page:1
            })
            that.verifierList();
          } else {
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1500
            });
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    })
    .catch(() => {
      
    });
    
  },
  onVerifierChange(event){;
    let index = event.currentTarget.dataset.index;
    let item = event.currentTarget.dataset.item;
    let overifierData = this.data.verifierList;
    overifierData[index].checked = event.detail;
    let that = this;
    let urlStr = app.getNetAddresss("plugin.coupon-store.frontend.clerk-manage.statusClerk");
    let json = {
      cstore_id:this.data.storeId,
      id:item.id,
      status:event.detail?1:0
    };
    console.log(this.data.storeId);
    app._postNetWork({
      url: urlStr,
      data:json,
      success: function (resdata) {
        let res = resdata.data;
        if (res.result == 1) {
          console.log(res);
          that.setData({
            verifierList : overifierData
          })
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() { //因为要返回上一页刷新数据
    this.verifierList();
  },
  onAddition() {
    wx.navigateTo({
      url: `/packageF/consumption/verifier/verifier?storeId=${this.data.storeId}`
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    wx.showNavigationBarLoading();
     //显示 loading 提示框。需主动调用 wx.hideLoading 才能关闭提示框
     wx.showLoading({
      title: '刷新中...',
    })
    this.setData({
      page:1
    })
    this.verifierList();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.isLoadMore) {
      this.getMoreData();
    } else {
      console.log('没有更多了');
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})